<?php
/**
 * 资讯模型
 * @DateTime 2017-09-04
 * @author xyl
*/
class Information_model extends MY_Model
{
	private $table = 'information';

	function __construct()
	{
		parent::__construct();
	}

	/**
	 * 动态获取资讯列表
	 * @DateTime 2017-09-05
 	 * @author xyl
	 * @param  string $limit [每页显示数量]
	 * @param  string $start [开始位置]
	 * @param  string $info  [信息]
	 * @return [array]        [list:数据主体 count:数据总数]
	 */
	public function ajaxGetInfosList($limit, $start, $get = false,$select = '*')
	{
		$where['status'] = 1;
		$like = array(); 
		$order = 'type asc,create_time desc';
		if(isset($get['type']) && $get['type']!='') $where['type'] = $get['type'];
		if(isset($get['name'])){
			$like['name'] = $get['name'];
		}
		if(isset($get['start_time']) && !empty($get['start_time'])) $where['create_time >='] = $get['start_time'];
		if(isset($get['end_time']) && !empty($get['end_time'])) $where['create_time <='] = $get['end_time'];
		$data = $this->hw_getList_like_limit($this->table,$where,$select,$like,$start,$limit,'both',$order);
		$total = $this->hw_count($this->table,$where,$select,$like);
		return array('data'=>$data,'total'=>$total);
	}
	
	/**
	 * 根据关键词查询符合条件的资讯
	 * @DateTime 2017-09-06
 	 * @author xyl
	 * @param  [type] $info [description]
	 * @return [type]       [description]
	 */
	public function getInfosList($get)
	{
		$like['theme'] = $get['keyword'];
		$where['type'] = 1;
		$where['status'] = 1;
		$list = $this->hw_getList_like($this->table,$where,'*',$like,'both','create_time desc');
		return $list;
	}
	/**
	 * 获取还可设置的轮播图的数量
	 * @DateTime 2017-09-05
 	 * @author xyl
	 * @return [type] [description]
	 */
	// public function getInfosnum()
	// {
	// 	$where['status'] = 1;
	// 	$where['type'] = 1;
	// 	$num = $this->db->from($this->table)->where($where)->count_all_results();
	// 	$left_num = 4-$num;
	// 	return $left_num;
	// }

	/**
	 * 获取单条资讯详情
	 * @DateTime 2017-09-05
 	 * @author xyl
	 * @param  [type] $where   [查询条件]
	 * @return [type]          [description]
	 */
	public function getInfoDetail($where,$is_rela = 0)
	{
		$info = $this->hw_getRecord($this->table,$where);
		$data['info'] = $info;
		$data['count'] = 2;
		if (!empty($info['relation_id']) && $is_rela==1) {
			$relation = explode(',', $info['relation_id']);
			$relation_info = array();
				//循环获取资讯详情
			for ($i=0; $i < count($relation); $i++) { 
				$relation_info[] = $this->hw_getRecord($this->table,array('id'=>$relation[$i]));
			}
			$data['relation'] = $relation_info;
			$data['count'] = 2-count($relation);
		}
		return $data;
	}

	/**
	 * 添加或更新资讯
	 * @DateTime 2017-09-05
 	 * @author xyl
	 * @param [type] $post [description]
	 */
	public function addInformation($post)
	{
		$data = array(
			'theme'			=>	$post['theme'],
			'default_img' 	=>	$post['default_img'],
			'content' 		=>	$post['details'],
			'link' 			=>	$post['link'],
		);
		$data['relation_id'] = '';
		if (isset($post['news']) && !empty($post['news'])) {
			$data['relation_id'] = implode(',',$post['news']);
		} 
		if (isset($post['id']) && !empty($post['id'])) {
			$data['update_time'] = date('Y-m-d H:i:s');
			$where['id'] = $post['id'];
			$before = $this->getInfoDetail($where);
			$log_details = array('before'=>$before,'after'=>$data);
			$res = $this->hw_Update($this->table,$data,$where);
			$msg = '编辑';
			$type = 2;
			$id = $post['id'];
		} else {
			$res = $this->hw_Insert($this->table,$data);
			$msg = '添加';
			$type = 1;
			$log_details = $data;
			$id = $res;
		}
		if ($res) {
			$result = array('code'=>1, 'msg'=>$msg.'资讯成功');
		} else {
			$result = array('code'=>0, 'msg'=>$msg.'资讯失败');
		}
		//添加操作日志
		$this->logMdl->addOperationLog($result['code'],$type,$id,$this->table,'资讯',json_encode($log_details));
		return $result;
	}

	/**
	 * 设置上线和下架
	 * @DateTime 2017-09-06
     * @author xyl
	 * @param [type] $post [description]
	 */
	public function setRoll($post)
	{
		$ids = $post['id'];
		$data['update_time'] = date('Y-m-d H:i:s');
		$data['type'] = $post['roll_type'];
		$log_details = array();
		$edit_ids = array();
		foreach ($ids as $k=>&$value) {
			$before = $this->hw_getRecord($this->table,array('id'=>$value));
			if($before['type']!=$data['type']){
				$log_details[] = array('before'=>$before,'after'=>$data);
				$edit_ids[] = $value;
			}    
		}

		$this->db->trans_begin();
		if(!empty($edit_ids) && count($edit_ids)>0){
			foreach ($edit_ids as $value) {
				$this->hw_Update($this->table,$data,array('id'=>$value));
			}
		}
		if ($this->db->trans_status() === FALSE)
		{
			$this->db->trans_rollback();
			$result = array('code'=>0,'msg'=>'操作失败');
		}
		else
		{
			$this->db->trans_commit();
			$result = array('code'=>1,'msg'=>'操作成功');
		}
		if(!empty($edit_ids) && count($edit_ids)>0){
			for ($i=0; $i < count($edit_ids) ;$i++) { 
				$detail = $log_details[$i];
				$this->logMdl->addOperationLog($result['code'],2,$edit_ids[$i],$this->table,'资讯状态',json_encode($detail));
			}	
		}
		return $result;
	}
	
}